package one.stand.controller;

import com.github.vioao.wechat.bean.response.BaseResponse;
import com.jmp.base.dto.AccountAppIdRequest;
import com.jmp.base.dto.AccountResponse;
import com.jmp.base.dto.WechatTemplateInitRequest;
import com.jmp.feign.AccountFeign;
import com.jmp.feign.WechatFeign;
import lombok.extern.slf4j.Slf4j;
import one.stand.enums.LoginEnum;
import one.stand.model.ResultModel;
import one.stand.request.ComponentProgramRequest;
import one.stand.request.ComponentVerifyTicketRequest;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: QIK
 * @CreateDate: 2019/5/9 14:41
 */
@RestController
@RequestMapping("wx")
@Slf4j
public class WxController extends BaseController {
    @Autowired
    private AccountFeign accountFeign;
    @Autowired
    private WechatFeign wechatFeign;

    @GetMapping("setWebViewDomain")
    public ResultModel setWebViewDomain(String appId, String urls) {
        log.info("setWebViewDomain appId={}", appId);
        BaseResponse response = domainService.setWebViewDomain(appId, urls);
        return ResultModel.success(response);
    }

    @GetMapping("addTemplate")
    public ResultModel addTemplate(String appId) {
        AccountAppIdRequest accountAppIdRequest = AccountAppIdRequest.builder()
                .appId(appId)
                .build();
        ResultModel<AccountResponse> accountResponseResultModel = accountFeign.getByAppId(accountAppIdRequest);
        AssertUtil.isTrue(accountResponseResultModel);
        AccountResponse model = accountResponseResultModel.getData();
        LoginEnum loginEnum = LoginEnum.getByAccountType(model.getAccountType());
        WechatTemplateInitRequest wechatTemplateInitRequest = WechatTemplateInitRequest.builder()
                .companyId(model.getCompanyId())
                .loginEnum(loginEnum)
                .appId(model.getAppId())
                .build();
        wechatFeign.templateInit(wechatTemplateInitRequest);
        return ResultModel.success();
    }

    @PostMapping("wxcallback")
    public String wxcallback(@RequestBody ComponentVerifyTicketRequest request) {
        return wxService.wxcallback(request);
    }

    @PostMapping("program")
    public String program(@RequestBody ComponentProgramRequest request) {
        return wxService.wxcallback(request);
    }
}
